/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2019-2019.
 * Description: support permissive mode of CFI feature.
 * Author: tengfei <tengfei22@huawei.com>
 * Create: 2019-06-22
 */

#ifndef __CFI_PERMISSIVE_MODE_H__
#define __CFI_PERMISSIVE_MODE_H__

#ifdef CONFIG_RTOS_CFI_MODE_PERMISSIVE
extern int sysctl_cfi_permissive_level;
extern int cfi_permissive_level_max;

#ifdef CONFIG_RTOS_HCFI
#ifdef CONFIG_RTOS_HCFI_FN_ONEARG
void notrace rtos_hcfi_logger(void *callee);
#else
void notrace rtos_hcfi_logger(const char *file, unsigned int line, const char *func, void *fptr);
#endif
#endif /* CONFIG_RTOS_HCFI */

#ifdef CONFIG_RTOS_PAC_CFI_PERMISSIVE
#include <asm/ptrace.h>
#define REG_LR_NUM 30
// when pointer auth error, second-top and third-top bits are corrupted by 01 or 10
#define pac_check_errcode(addr) \
	(((addr) & (1ULL << 62)) ^ ((addr) & (1ULL << 61)))
#define pac_check_bit(addr)		\
	(((addr) >> CONFIG_ARM64_VA_BITS) ^ ((~0ULL) >> CONFIG_ARM64_VA_BITS))

bool notrace rtos_pac_logger(const char *name, unsigned int pid_num,
						unsigned long pc, unsigned long lr);
bool get_pac_permissive_enabled(void);
bool fpac_fault_repair(struct pt_regs *regs, unsigned long esr);
#endif

#ifdef CONFIG_RTOS_BTI_USER_PERMISSIVE
void notrace rtos_bti_logger(const char *name, unsigned int pid_num, unsigned int tgid_num);
bool get_bti_permissive_enabled(void);
#endif

#endif /* CONFIG_RTOS_CFI_MODE_PERMISSIVE */

#endif /* __CFI_PERMISSIVE_MODE_H__ */
